var picCenterGlobal = {
	firstSelectTab1 : true,
	groupsCache : {},
	sortTypeJson : {
		0 : {
			sort : "",
			dir : ""
		},
		1 : {
			sort : "UPDATE_TIME",
			dir : "DESC"
		},
		2 : {
			sort : "UPDATE_TIME",
			dir : "ASC"
		},
		3 : {
			sort : "PIC_SIZE",
			dir : "DESC"
		},
		4 : {
			sort : "PIC_SIZE",
			dir : "ASC"
		}
	}
};
$(function() {
	document.domain = cookieDomain;// 'ailkcloud.com';
	$('#tabs').myTabs({
		tabsContainerId : 'tabsContent',
		border : false,
		width : '800px',
		onSelect : function(title, index) {
			if (index == 1 && picCenterGlobal.firstSelectTab1) {
				$('#doSearchBtn_pc').trigger('click');
				picCenterGlobal.firstSelectTab1 = false;
			}
		}
	});
	// 图片上传插件
	var myDropzone = new Dropzone(
			'body #fmUpload_pc',
			{
				url : mainDomain + '/picroom/uploadSinglePic.action',
				paramName : 'myfile',
				clickable : 'body #addPicBtn_pc',
				autoProcessQueue : false,
				addRemoveLinks : true,
				previewsContainer : 'body #imgPreview_pc',
				dictFallbackMessage : '您所使用的浏览器不支持图片上传，为了更方便进行上传图片，请使用Firefox、360或IE10+浏览器',
				dictDefaultMessage : '将文件拖拽至此区域进行上传（或点击此区域）',
				dictMaxFilesExceeded : '超过最大上传数量，该图片无法上传',
				dictCancelUpload : '停止上传',
				dictCancelUploadConfirmation : '确定停止上传此图片吗？',
				dictResponseError : '服务器出错，上传失败',
				dictRemoveFile : '删除',
				acceptedFiles : 'image/*',
				parallelUploads : 100,
				maxFilesize : 2,
				maxFiles : 100,
				success : function(file, rs) {
					if (rs.success) {
						if (rs.data.picResponse) {
							file.url = rs.data.picResponse.url;
							file.picRoomId = rs.data.picResponse.picRoomId;
						}
						if (file.previewElement) {
							var previewElement = file.previewElement;
							previewElement.classList.add("dz-success");
							$(previewElement).find('.dz-remove').remove();
							var $toRight = $('<a href="javascript:undefined;" class="dz-toright">加入右侧</a>');
							$toRight
									.appendTo($(previewElement))
									.bind(
											'click',
											function() {
												if ($toRight.html() == '已加到右侧') {
													return false;
												}
												var $li = $('<li objId="'
														+ file.url
														+ '" picSrc="'
														+ file.url
														+ '"><a href="javascript:void(0);"></a><p><img height="40px;" width="60px;" alt="'
														+ file.name
														+ '"  style="max-width:90%" src="'
														+ file.url
														+ '"></p><span class="img_name">'
														+ file.name
														+ '</span></li>');
												$('#insertListUl_pc').append(
														$li);
												$toRight.html('已加到右侧')
														.addClass('ready');
												$li
														.find('a')
														.click(
																function() {
																	$li
																			.remove();
																	$toRight
																			.html(
																					'加入右侧')
																			.removeClass(
																					'ready');
																});
												return false;

											});
							$(previewElement).click(function() {
								$toRight.trigger('click');
								return false;
							});
						}
					} else {
						renderUploadError(file, rs.message);
					}

				},
				error : function(file, message) {
					renderUploadError(file, message);
				}
			});
	// 进度
	myDropzone.on("totaluploadprogress", function(progress, totalBytes,
			totalBytesSent) {
		$("#progressBar_pc").css('width', (progress).toFixed(0) + '%');
		$('#allProgress_pc').html((progress).toFixed(0) + '%');
	});
	// 添加图片
	myDropzone.on("addedfile", function(file) {
		$('#imgPreviewTip_pc').hide();
		$('#submitUploadBtn_pc').removeClass('start_upload_unclickable')
				.addClass('start_upload_clickable');
	});
	// 上传完毕
	myDropzone.on("queuecomplete", function(file) {
		$('.upload_complete').show();
	});
	// 超过最大上传数量
	myDropzone.on("maxfilesexceeded", function(file) {
		this.removeFile(file);
		alert('已达到最大上传数量');
	});
	// 上传图片
	$('#submitUploadBtn_pc').click(
			function() {
				$('#upload_groupId_pc').val($('#groupSelect_pc').val());
				$('#upload_groupName_pc').val(
						$("#groupSelect_pc").find("option:selected").text());
				$(this).removeClass('start_upload_clickable').addClass(
						'start_upload_unclickable');
				if (myDropzone.getQueuedFiles().length == 0) {
					alert('请先添加需要上传的图片，可以点击或批量拖拽图片至预览区域进行上传');
					return;
				}
				var totalBytes = 0;
				for (var i = 0; i < myDropzone.getQueuedFiles().length; i++) {
					totalBytes += myDropzone.getQueuedFiles()[i].size;
				}
				$('#totalCounts_pc').html(myDropzone.getQueuedFiles().length);
				$('#totalSize_pc').html((totalBytes / 1024).toFixed(2));
				$('.upload_complete').hide();
				$("#progressBar_pc").css('width', '0%');
				$('#allProgress_pc').html('0%');
				myDropzone.processQueue();
			});
	// 点击预览区域触发新增图片按钮点击事件
	$('#imgPreview_pc').click(function() {
		$('#addPicBtn_pc').trigger('click');
	});
	// 插入图片拖动排序
	$("#insertListUl_pc,#insertListUl1_pc").dragsort({
		dragSelector : "li",
		dragEnd : function() {
		},
		dragBetween : false,
		placeHolderTemplate : "<li></li>"
	});

	// 点击插入事件
	$("#insertPicBtn_pc").click(
			function() {
				var urlMap = {};
				$("#insertListUl_pc").children('li').each(function(i) {
					var id = $(this).attr('objId');
					var picSrc = $(this).attr('picSrc');
					urlMap[id] = picSrc;
					$(this).find('a').trigger('click');
				});

				for ( var url in urlMap) {
					$(parent.document).find("form").append(
							"<input type=\"hidden\" value="
									+ JSON.stringify(url) + " name=\"coverPicUrl\"/>");
					break;
				}

				$('#dataCont_pic').data("urlMap", urlMap);
				$('#insertend_common', parent.document).trigger('click');
				$("#insertListUl_pc").empty();
			});
});
// 渲染上传错误信息
function renderUploadError(file, message) {
	// message="服务器出错，上传失败";
	var node, _i, _len, _ref, _results;
	if (file.previewElement) {
		file.previewElement.classList.add("dz-error");
		if (typeof message !== "String" && message.error) {
			message = message.error;
		}
		_ref = file.previewElement.querySelectorAll("[data-dz-errormessage]");
		_results = [];
		for (_i = 0, _len = _ref.length; _i < _len; _i++) {
			node = _ref[_i];
			_results.push(node.textContent = message);
		}
		return _results;
	}
}
// 加入到右侧
function addImg2right(id, picName, picSrc, picDpi) {
	var tempName = picName;
	if (picName.length > 13) {
		tempName = picName.substring(0, 13) + '...';
	}
	$('.checkAllItem_pc[objId="' + id + '"]').attr('checked', true);
	$('.picListItem_pc[objId="' + id + '"]').addClass('pro_pic_select').find(
			'sub').show();
	var $li = $('<li id="right_li_pc_'
			+ id
			+ '" objId="'
			+ id
			+ '" picSrc="'
			+ picSrc
			+ '"><a href="javascript:void(0);"></a><p><img height="40px;" width="60px;" alt="'
			+ picName + '"  style="max-width:90%" src="' + picSrc
			+ '"></p><span class="img_name" title="' + picName + '">'
			+ tempName + '<div style="color:green;">' + picDpi
			+ '</div></span></li>');
	$('#insertListUl1_pc').append($li);
	$li.find('a').click(function() {
		removeImg4right(id);
	});
}
// 从右侧移除
function removeImg4right(id) {
	$('.checkAllItem_pc[objId="' + id + '"]').removeAttr('checked');
	$('.picListItem_pc[objId="' + id + '"]').removeClass('pro_pic_select')
			.find('sub').hide();
	$('#right_li_pc_' + id).remove();
}
//
function dateTimeToLocalString(dateTime) {
	if (dateTime == null) {
		return "";
	}
	var date = new Date(dateTime);
	Y = date.getFullYear();
	M = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date
			.getMonth() + 1);
	D = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
	var hh = date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours();
	var mm = date.getMinutes() < 10 ? ("0" + date.getMinutes()) : date
			.getMinutes();
	return Y + '-' + M + '-' + D + ' ' + hh + ':' + mm;
}